import pageination from '../../components/pageination/pageination.vue'
export default {
	data() {
		return {
			userData: [],
			//数据
			total: 0, //总条数
			page: 1, //当前页
			size: 10, //每页显示条数
			fatherIndex: 0, //一级菜单index
		}
	},
	components: {
		pageination
	},
	created() { //调用方法
		let that = this;

	},
	mounted() {},
	updated:function(){
		if(this.getParamValue("fatherIndex")){
			this.fatherIndex=this.getParamValue("fatherIndex");
		}
	},
	methods: { //写函数方法
		getData: function() {
			let that = this;
			that.$post(
					that.urls.findAllRole, {
						"pageNum": that.page,
						"pageSize": that.size
					}
				)
				.then(function(res) {
					if(res.success) {
						that.userData = res.pageInfo.list;
						that.total = res.pageInfo.total;
						that.page = res.pageInfo.pageNum;
					}
				})
		},
		//页码切换执行方法
		pageFn: function(val, pid) {
			this.page = parseInt(val);
      this.getData();
			//此处调列表数据接口
		},
		/**返回子组件用户选择的一页显示条数**/
		obtainChild: function(data) {
			this.row = data;
		},
		//删除
		deleteRole: function(id) {
			if(id != null) {
				let that = this;
				that.$post(
						that.urls.removeRole, {
							"id": id
						}
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							msg = '删除角色成功！';
							that.getData();
						} else {
							msg = '删除角色失败！';
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
		toDateStr(timestamp) {
			let time = new Date(timestamp) // 先将时间戳转为Date对象，然后才能使用Date的方法
			let year = time.getFullYear(),
				month = time.getMonth() + 1, // 月份是从0开始的
				day = time.getDate();
			//  hour = time.getHours(),
			//  minute = time.getMinutes(),
			//  second = time.getSeconds();
			//  return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
			return year + '-' + month + '-' + day;
		},
		searchRole: function() {
			let that = this;
			let value = $('#searchRoleVal').val();

			that.$post(
					that.urls.findAllRole, {
						"pageNum": 0,
						"pageSize": 7,
						roleName: value,
					}
				)
				.then(function(res) {
					if(res.success) {
						that.userData = res.pageInfo.list;
						that.total = res.pageInfo.total;
						that.page = res.pageInfo.pageNum;
						that.size = res.pageInfo.size;
					}
				})

		},
		//编辑角色
		editorRole: function(id, name){
      this.set("operType", 0);
			if(id != null && id != "") {
				this.set("addRoleid", id);
        this.set("roleName", name);
				this.$router.push({
					path: "addRole?fatherIndex="+this.fatherIndex,
				})
			}else{
				this.remove("addRoleid", "");
        this.set("addRoleid", null);
        this.set("roleName", null);
				this.$router.push({
					path: "/addRole?fatherIndex="+this.fatherIndex,
				})
			}

		},
		//查看角色
		viewRole: function(id, name) {
      console.log('查看角色', + id + name)
      this.set("addRoleid", id);
      this.set("roleName", name);
      this.set("operType", 1);
			this.$router.push({
				path: "/addRole?fatherIndex="+this.fatherIndex,
			})
		},
    
    // 修改角色状态 
    editStatus: function (id, sta) {
      let that = this;
      that.$post(
        that.urls.editRole,
        {"id": id, "roleStatus": sta}
      )
        .then(function(res) {
          if(res.success) {
            layer.msg('修改角色状态成功!', {
              time: 3000, //3s后自动关闭
            });
            that.getData();
          }else {
            layer.msg('修改角色状态失败!', {
              time: 3000, //3s后自动关闭
            });
          }
        })
    }
    
	}
}
